package com.yanqu.road.server.cmd.activity.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.rank.UnionActivityRankListModel;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_ACTIVITY_UNION_RANK, desc = "商会盐场活动积分排行榜")
public class OreWarActivityUnionRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OreWarActivityProto.OreWarActivityUnionRankListReqMsg reqMsg = OreWarActivityProto.OreWarActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        OreWarActivityProto.OreWarActivityUnionRankListRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityUnionRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        ActivityInfo openActivityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if (null != openActivityInfo) {
//            if(openActivityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
//                UnionActivityRankListModel rankListModel = OreWarActivityRankMgr.getUnionRankListModel(openActivityInfo.getActivityId());
//                if (null != rankListModel) {
//                    respMsg.setRankList(rankListModel.getRankListMsg());
//                    UnionActivityConditionData unionActivityConditionData = OreWarActivityRankMgr.getUnionActivityConditionData(openActivityInfo.getActivityId(),
//                            player.getUserId());
//                    if(null != unionActivityConditionData) {
//                        respMsg.setMyRank(rankListModel.getMyRank(unionActivityConditionData.getUnionUid()));
//                        respMsg.setValue(unionActivityConditionData.getValue().toString());
//                    }
//                }
//            }else if(openActivityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
              /*  UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = CrossDataMgr.getCrossRankActivityUnionRankMsg(activityId);
                int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                int conditionType = OreWarActivityMgr.CONDITION_TYPE_UNION;
                maxRank = OreWarActivityMgr.getMaxRankByType(activityId, conditionType);
                UnionActivityConditionData unionActivityConditionData = OreWarActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
                while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank){
                    rankListMsg.removeRankList(rankListMsg.getRankListCount() -1 );
                }
                respMsg.setRankList(rankListMsg);
                int rank = -1;
                if(null != unionActivityConditionData) {
                    respMsg.setValue(unionActivityConditionData.getValue().toString());
                    if(!StringUtils.isNullOrEmpty(unionActivityConditionData.getUnionUid())){
                        rank = ActivityMgr.getMyCrossUnionRank(unionActivityConditionData.getUnionUid(), rankListMsg);
                    }
                }else {
                    respMsg.setValue("0");
                }
                respMsg.setMyRank(rank);*/
                /*String unionId = "";
                String localValue = "0";
                UnionActivityConditionData ucd = OreWarActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
                if (null != ucd) {
                    unionId = ucd.getUnionUid();
                    localValue = ucd.getValue().toString();
                }
                CrossActivityProto.CrossUnionActivtyRankReqMsg.Builder req = CrossActivityProto.CrossUnionActivtyRankReqMsg.newBuilder();
                req.setActivtyId(activityId);
                req.setConditionType(0);
                req.setLocalValue(localValue);
                req.setUnionId(unionId);
                player.sendChannelPacket(CrossProtocol.C2_CROSS_GET_ORE_ACTIVITY_UNION_RANK, req);
                return;*/
//                CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg.newBuilder();
//                msg.setActivityId(activityId);
//                msg.setUserId(player.getUserId());
//                msg.setConditionType(OreWarActivityMgr.CONDITION_TYPE_UNION);
//                msg.setRankListType(5);
//                player.sendPacket(CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_LIST, msg);
//                return;

            YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
            builder.setActivityId(activityId);
            builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
            builder.setIdKey(String.valueOf(OreWarActivityMgr.CONDITION_TYPE_UNION));
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_LIST, builder, activityId);
            return;
        }
        player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_UNION_RANK, respMsg);
    }
}
